package com.alibaba.cirtus.ots.application.app.ability.facade.v2serviceimpl;

import com.alibaba.citrus.ots.common.util.DateUtils;
import com.alibaba.cz.base.tool.log.Log;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.lattice2.epoch.sdk.model.Result;
import com.epoch.app.bcorder.model.dto.QueryGoodsListRequest;
import com.epoch.app.bcorder.model.dto.QueryGoodsListResponse;
import com.epoch.app.bcorder.model.dto.SearchStoreTerminalForPullRequest;
import com.epoch.app.bcorder.model.dto.WarehouseInfo;
import com.epoch.app.bcorder.service.PlatformEnhanceService;
import com.epoch.app.bcorder.service.UnifiedInventoryService;
import com.epoch.app.crpricecenter.api.creditquery.dto.QueryCompanyToGroupCreditListRequest;
import com.epoch.app.crpricecenter.api.creditquery.service.CreditQueryService;
import com.epoch.app.crpricecenter.model.dto.CompanyToGroupCreditResponse;
import com.epoch.app.otsapplicationappsdk.groupcredit.dto.AsyncRequest;
import com.epoch.app.otsapplicationappsdk.groupcredit.service.GroupCreditService;
import com.epoch.app.otsapplicationappsdk.networkinventorylist.dto.PageQueryStoreInventoryRequest;
import com.epoch.app.otsapplicationappsdk.networkinventorylist.dto.PageQueryStoreInventoryResponse;
import com.epoch.app.otsapplicationappsdk.networkinventorylist.dto.RenderRequest;
import com.epoch.app.otsapplicationappsdk.networkinventorylist.service.NetworkInventoryListService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class GroupCreditListV2ServiceImpl implements GroupCreditService {

    private static Log log = Log.getLogger(GroupCreditListV2ServiceImpl.class);

    private final static int PAGE_SIZE_100 = 100;

    @Autowired
    private CreditQueryService creditQueryService;

    @Override
    public Object render() {
        JSONObject result = new JSONObject();
        try {
            QueryCompanyToGroupCreditListRequest queryCompanyToGroupCreditListRequest = new QueryCompanyToGroupCreditListRequest();
            queryCompanyToGroupCreditListRequest.setLimit(PAGE_SIZE_100);
            Result<List<CompanyToGroupCreditResponse>> response = creditQueryService.queryCompanyToGroupCreditList(queryCompanyToGroupCreditListRequest);
            if (!response.isSuccess()){
                throw new Exception("调用分子公司信贷接口失败");
            }
            result.put("success",true);
            result.put("itemList",response.getResult());
        }catch (Exception e){
            result.put("success", false);
            result.put("msg",e.getMessage());
        }
        log.info("NetworkInventoryListServiceImpl_render_result={}", JSON.toJSONString(result));
        return result;
    }

    @Override
    public Object async(AsyncRequest asyncRequest) {
        Object requestBody = asyncRequest.getRequestInput();
        if (requestBody == null) {
            return null;
        }
        JSONObject result = new JSONObject();
        try {
            String companyId = null;
            String soldPartName = null;
            JSONObject jsonObject = JSONObject.parseObject(JSON.toJSONString(requestBody));
            log.info("async_jsonObject" + JSON.toJSONString(jsonObject));
            JSONObject fields = jsonObject.getJSONObject("fields");
            JSONArray selectOptions = fields.getJSONArray("selectOptions");
            soldPartName = fields.getString("value");
            if (org.apache.commons.collections4.CollectionUtils.isNotEmpty(selectOptions)){
                Object selectOne = selectOptions.get(0);
                JSONObject selectOneJson = JSONObject.parseObject(JSON.toJSONString(selectOne));
                JSONObject selected = selectOneJson.getJSONObject("selected");
                if (MapUtils.isNotEmpty(selected)){
                     companyId = selected.getString("value");
                }
            }

            QueryCompanyToGroupCreditListRequest queryCompanyToGroupCreditListRequest = new QueryCompanyToGroupCreditListRequest();
            queryCompanyToGroupCreditListRequest.setLimit(100);
            queryCompanyToGroupCreditListRequest.setCompanyId(companyId);
            queryCompanyToGroupCreditListRequest.setSoldPartName(soldPartName);
            Result<List<CompanyToGroupCreditResponse>> response = creditQueryService.queryCompanyToGroupCreditList(queryCompanyToGroupCreditListRequest);
            if (!response.isSuccess()){
                throw new Exception("调用分子公司信贷接口失败");
            }
            result.put("success",true);
            result.put("itemList",response.getResult());
        } catch (Exception e) {
            result.put("success", false);
            result.put("msg", e.getMessage());
        }
        log.info("NetworkInventoryListServiceImpl_async_result={}", JSON.toJSONString(result));
        return result;
    }



}
